package com.lanou3g.bookstore.category.dao;

import com.lanou3g.bookstore.category.domain.Category;
import com.lanou3g.bookstore.util.util.JDBCUtil;
import com.lanou3g.bookstore.util.util.MyResultSet;
import com.lanou3g.bookstore.util.util.MyStatement;

import java.util.ArrayList;
import java.util.List;

public class CategoryDao {
    //查询所有的分类
    public List<Category> findAll() {
        List<Category> categories = new ArrayList<>();
        String sql = "select * from category";
        MyStatement statement = JDBCUtil.getStatement(sql);
        MyResultSet rs = statement.executeQuery();
        while (rs.next()) {
            Category category = new Category();
            category.setCname(rs.getString("cname"))
                    .setCid(rs.getString("cid"));
            categories.add(category);
        }
        rs.close();
        statement.close();
        return categories;
    }

//    //找cid最大值
//    public Integer findMaxCid() {
//        String sql = "select max(cid) from category";
//        MyStatement statement = JDBCUtil.getStatement(sql);
//        MyResultSet rs = statement.executeQuery();
//        Integer cid = null;
//        if (rs.next()) {
//            cid = rs.getInt("max(cid)");
//        }
//        rs.close();
//        statement.close();
//        return cid;
//    }

    //重名
    public boolean hasName(String cname) {
        String sql = "select count(cname) from category where cname = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, cname);
        MyResultSet rs = statement.executeQuery();
        rs.next();
        int count = rs.getInt(1);
        rs.close();
        statement.close();
        return count > 0;
    }

    //添加类别
    public void add(Category category) {
        String sql = "insert into category values (?, ?)";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, category.getCid());
        statement.setString(2, category.getCname());
        statement.executeUpdate();
        statement.close();
        JDBCUtil.closeConn();
    }

    //查找给类别有没有书
    public boolean findByCategory(String cid) {
        String sql = "select count(*) from book where cid = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, cid);
        MyResultSet rs = statement.executeQuery();
        rs.next();
        int count = rs.getInt(1);
        rs.close();
        statement.close();
        return count > 0;
    }


    //删除类别
    public void del(String cid) {
        String sql = "delete from category where cid = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, cid);
        statement.executeUpdate();
        statement.close();
        JDBCUtil.closeConn();
    }

    //查找该类
    public Category findByCid(String cid) {
        Category category = new Category();
        String sql = "select * from category where cid = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, cid);
        MyResultSet rs = statement.executeQuery();
        rs.next();
        category.setCid(rs.getString("cid"))
                .setCname(rs.getString("cname"));
        rs.close();
        statement.close();
        return category;
    }

    public void edit(Category category) {
        String sql = "update category set cname = ? where cid = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, category.getCname());
        statement.setString(2, category.getCid());
        statement.executeUpdate();
        statement.close();
        JDBCUtil.closeConn();
    }
}
